package com.jaiky.imagespickers.utils;

import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.data.usage.DataUsage;
import ohos.data.usage.MountState;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Utils {
    /**
     * exist SDCard
     *
     * @return boolean
     */
    public static boolean existSDCard() {
        return DataUsage.getDiskMountedStatus().equals(MountState.DISK_MOUNTED);
    }

    /**
     * get Image Name
     *
     * @return getImageName
     */
    public static String getImageName() {
        String PATTERN = "yyyyMMddHHmmss";
        return new SimpleDateFormat(PATTERN, Locale.CHINA).format(new Date()) + ".jpg";
    }

    /**
     * 根据屏幕宽度与密度计算GridView显示的列数， 最少为三列，并获取Item宽度
     *
     * @param ability 上下文对象
     * @return 图片宽度
     */
    public static int getImageItemWidth(Context ability) {
        DisplayManager displayManager = DisplayManager.getInstance();
        Display display = displayManager.getDefaultDisplay(ability).get();
        int screenWidth = display.getAttributes().width;
        int densityDpi = display.getAttributes().densityDpi;
        int cols = screenWidth / densityDpi;
        cols = Math.max(cols, 3);
        int columnSpace = (int) (2 * display.getAttributes().densityPixels);
        return (screenWidth - columnSpace * (cols - 1)) / cols;
    }

}